package cn.edu.ctbu.datajpa.webapi;

import cn.edu.ctbu.datajpa.helper.PageUtils;
import cn.edu.ctbu.datajpa.domain.Student;
import cn.edu.ctbu.datajpa.service.StudentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {

    @Autowired
    public StudentService studentService;

    /**
     * 读取的url；/webapi/student/list
     * @return
     */
    @GetMapping("/list")
    public List<Student> getAll(){

        List<Student> students=studentService.findAll();
        return students;

    }


    /**
     * 分页读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "name",defaultValue = "") String name){

        //按id排倒序
        Sort sort=Sort.by(Sort.Direction.ASC,"id");

        Page<Student> studentPage;

        if(StringUtils.isEmpty(name))
        {
            Pageable pageable=PageRequest.of(page,size,sort);
            studentPage=studentService.findAll(pageable);
            //studentPage=studentService.findByNoAndPassword(pageable,no,password);
        }

        else
        {
            Student student=new Student();
            student.setName(name);

            Pageable pageable=PageRequest.of(page,size,sort);

            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Student> example=Example.of(student,matcher);

            studentPage=studentService.findAll(example,pageable);
        }

        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());

        return pageUtils;
    }


    /**
     * 新增学生的方法 /webapi/student/insert
     * @param student
     * @return
     */
    @PostMapping("/insert")
    public Student insert(Student student){

        Student student1=studentService.insert(student);
        return student1;

    }

    /**
     * 更新学生的方法 /webapi/student/update
     * @param student
     * @return
     */
    @PutMapping("/update")
    public Student update(Student student){

        Student oldstudent=studentService.getById(student.getId()); //读取旧的数据
        if(StringUtils.isEmpty(student.getPassword())){

            //没有新密码，就用旧的密码
            student.setPassword(oldstudent.getPassword());

        }
        student=studentService.update(student);
        return student;
    }

    /**
     * 删除学生的方法 /webapi/student/delete
     *
     * @param id
     */
    @DeleteMapping ("/delete/{id}")
    public void delete(@PathVariable Integer id){

        studentService.delete(id);

    }

    /**
     * 读取的url；/webapi/student/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id){

        Student student=studentService.getById(id);
        //student.setPassword("");
        return student;

    }

}